<?php
class LoginController extends CBaseController
{
	
	protected $validator;
	
	public function __construct(&$config=null) 
    { 
      parent::__construct($config);	  
    } 
	
	public function init($tpl)
	{
		parent::init($tpl);
		$this->templator->addViewsDir(dirname(__FILE__).'/../views');
	}
	
	public function index()
	{
		$this->templator->display('login.html');
	}
	
	public function logout()
	{
		$_user=$this->getObject('CurrentUser');
		$_user->logout();
		$this->getObject('CurrentRequest:Request')->redirect('login');
	}
	
	public function home()
	{
		$this->templator->display('dashboard.html');
	}
	
	public function process()
	{
		$validator = new FormValidator();;
		$validator->addValidation("username","req","Please fill in Name");
		$validator->addValidation("password","req","Please fill in Password");					
		if($validator->ValidateForm())
		{	
			$userDetail =R::findOne('userdetail',' username= ? ',array($_POST['username']));
			if(isset($userDetail))
			{
				if($userDetail->pass==$_POST['password'])
				{
					$_user=$this->getObject('CurrentUser');
					$_user->login($userDetail->id,$userDetail->username);
					$this->templator->display('dashboard.html');
				}
				else
				{
					$this->getObject('CurrentRequest:Request')->redirect('login');
				}
			}
			else
			{
				$this->getObject('CurrentRequest:Request')->redirect('login');
			}
		}
		else
		{
			$this->getObject('CurrentRequest:Request')->redirect('login');
		}
		
	}
	
}	